﻿using CourseManagement.Service;
using CourseManagerment.Common;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CourseManagerment.ViewModel
{
    public class FirstPageViewModel : NotifyBase
    {
        readonly Random random = new();
        readonly List<Task> tasks = new();
        private int _instrumentValue;
        private int _itemCount;

        bool taskSwitch = true;

        public FirstPageViewModel()
        {
            this.RefreshInstrumentValue();

            this.InitCourseSeries();
        }

        public ObservableCollection<CourseSeriesModel> CourseSeriesList { get; set; } = new();

        /// <summary>
        /// 仪表盘数据
        /// </summary>
        public int InstrumentValue
        {
            get { return _instrumentValue; }
            set { _instrumentValue = value; this.DoNotify(); }
        }
        /// <summary>
        /// 数据表的条目数量
        /// </summary>
        public int ItemCount
        {
            get { return _itemCount; }
            set { _itemCount = value; this.DoNotify(); }
        }
        public void Dispose()
        {
            try
            {
                taskSwitch = false;
                Task.WaitAll(this.tasks.ToArray());
            }
            catch
            {
            }
        }

        /// <summary>
        /// 数据表的条目
        /// </summary>
        private void InitCourseSeries()
        {
#if DEBUG
            CourseSeriesList.Add(new CourseSeriesModel
            {
                CourseName = "Java高级实战VIP班",
                SeriesColection = new LiveCharts.SeriesCollection { new PieSeries {
                    Title="ZHAOXI",
                    Values=new ChartValues<ObservableValue>{ new ObservableValue (123)},
                    DataLabels=false},new PieSeries {
                    Title="woyao",
                    Values=new ChartValues<ObservableValue>{ new ObservableValue (123)},
                    DataLabels=false} },
                SeriesList = new ObservableCollection<SeriesModel>
                {
                    new SeriesModel {SeriesName="B站",CurrentValue=161,IsGrowing=false,ChangeRate=75},
                    new SeriesModel {SeriesName="云课堂",CurrentValue=161,IsGrowing=true,ChangeRate=75},
                    new SeriesModel {SeriesName="云课堂",CurrentValue=161,IsGrowing=false,ChangeRate=75},
                    new SeriesModel {SeriesName="云课堂",CurrentValue=161,IsGrowing=false,ChangeRate=75},
                    new SeriesModel {SeriesName="英语",CurrentValue=161,IsGrowing=false,ChangeRate=75}
                }
            });
            this.ItemCount = 5;//动态更新条目数量
#else
            var cList = LocalDataAccess.GetInstance().GetCoursePlayRecord();
            this.ItemCount = cList.Max(c => c.SeriesList.Count);//动态更新条目数量
            foreach (var item in cList)
                this.CourseSeriesList.Add(item);
#endif

        }
        /// <summary>
        /// 更新仪表盘数据
        /// </summary>
        private void RefreshInstrumentValue()
        {
            var task = Task.Factory.StartNew(new Action(async () =>
            {
                while (taskSwitch)
                {
                    InstrumentValue = random.Next(Math.Max(this.InstrumentValue - 5, -10),
                        Math.Min(InstrumentValue + 5, 100));
                    await Task.Delay(1000);
                }
            }));
            tasks.Add(task);
        }
    }
}
